#include "HardwareTwi.h"
#include "../../../bit/bit.h"


void HardwareTwi::setBitRate(unsigned char rate)
{
	TWBR = rate;
}


void HardwareTwi::clearInterruptFlag()
{
	TWCR = SetBit(TWCR, TWINT, 0);
}

void HardwareTwi::setEnableAck(unsigned char flag)
{
	TWCR = SetBit(TWCR, TWEA, flag);
}


void HardwareTwi::setMasterMode(unsigned char flag)
{
	TWCR = SetBit(TWCR, TWSTA, flag);
}

void HardwareTwi::setStopCondition(unsigned char flag)
{
	TWCR = SetBit(TWCR, TWSTO, flag);
}


void HardwareTwi::setWriteCollision(unsigned char flag)
{
	TWCR = SetBit(TWCR, TWWC, flag);
}

void HardwareTwi::setStop(unsigned char flag)
{
	TWCR = SetBit(TWCR, TWSTO, flag);
}

void HardwareTwi::setEnable(unsigned char flag)
{
	TWCR = SetBit(TWCR, TWEN, flag);
}

void HardwareTwi::setInterrupt(unsigned char flag)
{
	TWCR = SetBit(TWCR, TWIE, flag);
}

unsigned char HardwareTwi::getStatus()
{
	return TWSR & 0xF8;
}

void HardwareTwi::setPrescaler(unsigned char flag)
{
	switch(flag)
	{
		case PRESCALER_1_TWI:
			TWSR = SetBit(TWSR, TWPS1, 0);
			TWSR = SetBit(TWSR, TWPS0, 0);
			break;
		case PRESCALER_4_TWI:
			TWSR = SetBit(TWSR, TWPS1, 0);
			TWSR = SetBit(TWSR, TWPS0, 1);
			break;
		case PRESCALER_16_TWI:
			TWSR = SetBit(TWSR, TWPS1, 1);
			TWSR = SetBit(TWSR, TWPS0, 0);
			break;
		case PRESCALER_64_TWI:
			TWSR = SetBit(TWSR, TWPS1, 1);
			TWSR = SetBit(TWSR, TWPS0, 1);
			break;
		default:
			TWSR = SetBit(TWSR, TWPS1, 0);
			TWSR = SetBit(TWSR, TWPS0, 0);
			break;
	}
}

void HardwareTwi::setDataToTransmit(unsigned char data)
{
	TWDR = data;
}

void HardwareTwi::setAddress(unsigned char address)
{
	TWAR = (TWAR & 0x01) | (address<<1);
}

void HardwareTwi::setCallRecognition(unsigned char flag)
{
	TWAR = SetBit(TWAR, TWGCE, flag);
}




